package controller;

import gui.MainFrame;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.ContainerController;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import componenti.buffer.Buffer;
import componenti.buffer.Entity;
import componenti.comunication.SimulationFacade;

public class Administrator {

	public static void main(String[] args) {
		setLookAndFeel();
		setOutput();
		ContainerController cc = launchContainer();
		Buffer<Entity> buffer = new Buffer<Entity>();
		SimulationFacade sf = new SimulationFacade(buffer);
		MainFrame mf = new MainFrame(null, null, cc, sf);
		mf.setVisible(true);
	}

	private static void setOutput() {
		try {
			System.setOut(new PrintStream(new File("Output.txt")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}

	private static void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	private static ContainerController launchContainer() {
		String command = "java jade.Boot -gui";
		try {
			java.lang.Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String host = "localhost";
		String port = "1077";
		Runtime rt = Runtime.instance();
		Profile p = new ProfileImpl();
		p.setParameter(Profile.MAIN_HOST, host);
		p.setParameter(Profile.MAIN_PORT, port);
		return rt.createMainContainer(p);

	}

}
